/**   
* @Title: JqGridSearchOperation.java 
* @Package com.csis.framework.util.jqgrid 
* @Description: TODO
* @author dailey  
* @date 2012-10-7
* @version V1.0   
*/
package com.csis.framework.util.jqgrid;

import com.csis.framework.domain.Condition;

/**
 * @author  dailey
 */
public class JqGridSearchOperation implements Condition {
	/**
	 * 
	 */
	public String searchField;
	/**
	 * 
	 */
	public String searchOper;
	/**
	 * 
	 */
	public String searchString;

	public JqGridSearchOperation(JqGridModel gridModel) {
		super();
		this.searchField = gridModel.searchField;
		this.searchOper = gridModel.searchOper;
		this.searchString = gridModel.searchString;
	}

	String preOperationSql() {

		String result = "";
		if ("nu".equals(searchOper.trim())) {
			result = searchField + " IS NULL ";
			return result;
		} else if ("nn".equals(searchOper.trim())) {
			result = searchField + " IS NOT NULL ";
			return result;
		}
		if ((searchString == null || searchString.trim().length() == 0))
			return "";
		if (searchOper.trim().equals("eq")) //等于           
			result = searchField + "='" + searchString + "' ";
		else if (searchOper.trim().equals("ne")) //不等于         
			result = searchField + " != '" + searchString + "' ";
		else if (searchOper.trim().equals("lt")) //小于           
			result = searchField + " < '" + searchString + "' ";
		else if (searchOper.trim().equals("le")) //小于等于          
			result = searchField + " <= '" + searchString + "' ";
		else if (searchOper.trim().equals("gt")) //大于            
			result = searchField + " > '" + searchString + "' ";
		else if (searchOper.trim().equals("ge")) //大于等于          
			result = searchField + " >= '" + searchString + "' ";
		else if (searchOper.trim().equals("bw")) //以...开始            
			result = searchField + " LIKE '" + searchString + "%' ";
		else if (searchOper.trim().equals("bn")) //不以...开始           
			result = searchField + " NOT LIKE '" + searchString + "%' ";
		else if (searchOper.trim().equals("in")) { //包括            
			String[] sv = searchString.split(",");
			String svString = "";
			for (int i = 0; i < sv.length; i++) {
				svString += " '" + sv[i] + "' ";
				if (i < sv.length - 1) {
					svString += ",";
				}
			}
			result = searchField + " IN ( " + svString + " ) ";
		} else if ("ni".equals(searchOper.trim())) {//不包括
			String[] sv = searchString.split(",");
			String svString = "";
			for (int i = 0; i < sv.length; i++) {
				svString += " '" + sv[i] + "' ";
				if (i < sv.length - 1) {
					svString += ",";
				}
			}
			result = searchField + " NOT IN ( " + svString + " ) ";
		} else if ("ew".equals(searchOper.trim())) {//ends with
			result = searchField + " LIKE '%" + searchString + "' ";
		} else if ("en".equals(searchOper.trim())) {//does not end with
			result = searchField + " NOT LIKE '%" + searchString + "' ";
		} else if ("cn".equals(searchOper.trim())) {//contains
			result = searchField + " LIKE '%" + searchString + "%' ";
		} else if ("nc".equals(searchOper.trim())) {//does not contain
			result = searchField + " NOT LIKE '%" + searchString + "%' ";
		} else if ("nu".equals(searchOper.trim())) {//is null
			result = searchField + " IS NULL ";
		} else if ("nn".equals(searchOper.trim())) {//is null
			result = searchField + " IS NOT NULL ";
		}
		return result;

	}

	/**
	 * 
	 */
	public JqGridSearchOperation() {
	}

	public String getSearchField() {
		return searchField;
	}

	public void setSearchField(String searchField) {
		this.searchField = searchField;
	}

	public String getSearchOper() {
		return searchOper;
	}

	public void setSearchOper(String searchOper) {
		this.searchOper = searchOper;
	}

	public String getSearchString() {
		return searchString;
	}

	public void setSearchString(String searchString) {
		this.searchString = searchString;
	}

	/* (non-Javadoc)
	 * @see com.csis.framework.domain.Condition#getCondition()
	 */
	@Override
	public String getCondition(String sql) {
		if (sql.toLowerCase().contains("where")) {
			String cond = preOperationSql();
			if ("".equals(cond) || "".equals(cond.trim())) {
				return sql;
			}
			return sql + " AND " + preOperationSql();
		}
		return sql;
	}

}